package com.minshenglife.zanzan.web.power;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.entity.power.ManagerEmp;
import com.minshenglife.zanzan.entity.power.ManagerList;
import com.minshenglife.zanzan.service.power.ManagerEmpService;
import com.minshenglife.zanzan.service.power.UserPowerService;

/**
 * 用户权限controller
 * 
 * @author wangpeng
 * 
 */
@Controller
@RequestMapping("/userLogin")
public class ManagerEmpController {
	Logger log = LoggerFactory.getLogger(ManagerEmpController.class);

	@Resource
	ManagerEmpService managerEmpService;
	@Resource
	UserPowerService userPowerService;

	/**
	 * 用户登陆
	 * 
	 * @param request
	 * @param userName
	 * @param passWord
	 * @return
	 */
	@RequestMapping(value = "/userLogin.power", method = RequestMethod.POST)
	public String Userlogin(ModelMap modeMap, HttpServletRequest request, HttpServletResponse response, String userName, String passWord) {
		log.info("start    Userlogin()");
		ManagerEmp managerEmp = managerEmpService.findMangerEmp(userName, passWord);
		//System.out.println(managerEmp);
		if (managerEmp != null && managerEmp.getEmpId() != null) {
			HttpSession session = request.getSession(true);
			session.setMaxInactiveInterval(20 * 60);// 有效期20分钟
			session.setAttribute("managerEmp", managerEmp);
			ManagerEmp manager = (ManagerEmp) session.getAttribute("managerEmp");
			modeMap.addAttribute("loginUser", manager);
			return "/jsp/power/main";
		} else {
			modeMap.addAttribute("error", "用户名或密码错误，请重新输入！");
//			return "/jsp/power/managlogin";
			return "/manage/login";
		}
	}

	@RequestMapping(value = "/addManager.power", method = RequestMethod.POST)
	public String addManagerEmp(ManagerEmp managerEmp, ModelMap map) {
		log.info("start    addManagerEmp()");
		return "/jsp/power/addManager";
	}

	/**
	 * 添加管理员
	 * 
	 * @param modelMap
	 * @param request
	 * @param manager
	 * @return
	 */
	@RequestMapping(value = "/insertManager.power", method = RequestMethod.POST)
	public String insertManager(ModelMap modelMap, HttpServletRequest request, ManagerEmp manager) {
		log.info("start    insertManager()");
		HttpSession session = request.getSession();
		ManagerEmp managerEmp = (ManagerEmp) session.getAttribute("managerEmp");
		if (managerEmp.getGrade().equals("1")) {
			ManagerEmp isExist = managerEmpService.findId(manager.getEmpId());
			if (isExist != null && isExist.getDept_id() != null) {

			} else {
				managerEmpService.insertManagerEmp(manager);
			}
		} else {
		}
		return "/jsp/power/results";
	}

	/**
	 * 删除管理员
	 * 
	 * @param request
	 * @param empId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteManagerEmp.power", method = RequestMethod.GET)
	public String deleteManagerEmp(HttpServletRequest request, String empId) {
		log.info("start    deleteManagerEmp()");
		ManagerEmp managerEmp = managerEmpService.findId(empId);
//		if(!empId.equals("86330708")){
		if(managerEmp != null && managerEmp.getGrade() != null && !managerEmp.getGrade().equals("1")){
			managerEmpService.deleteManager(empId);
			String str = "删除成功";
			return str;
		}else {
			String str = "删除失败...高级管理员不能删除";
			return str;
		}
	}
	/**
	 * 跳转到更改密码
	 * @param modeMap
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/skipupdate.power", method = RequestMethod.GET)
	public String skipupdate(ModelMap modelMap, HttpServletRequest request) {
		log.info("start    skipupdate()");
		HttpSession session = request.getSession();
		ManagerEmp managerEmp = (ManagerEmp) session.getAttribute("managerEmp");
		if(managerEmp!= null){
			if("1".equals(managerEmp.getGrade())){
				modelMap.addAttribute("empId",managerEmp.getEmpId());
				modelMap.addAttribute("emp_name",managerEmp.getEmpId());
			}else{
				User user = userPowerService.findUserId(managerEmp.getEmpId());
				modelMap.addAttribute("empId",user.getEmpId());
				modelMap.addAttribute("emp_name",user.getEmp_name());
			}
			
			//System.out.println(user.getEmp_name());
			return "/jsp/power/updatePassWord";
		}else{
//			return "/jsp/power/managlogin";
			return "/manage/login";
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param modelMap
	 * @param request
	 * @param empId
	 * @param formerPassWord
	 * @param newPassword
	 * @return
	 */
	@RequestMapping(value = "/updatePassWord.power", method = RequestMethod.POST)
	public String UpdatePassWork(ModelMap modelMap, HttpServletRequest request, String empId, String formerPassWord, String newPassword) {
		log.info("start    UpdatePassWork()");
		//System.out.println(empId+formerPassWord);
		ManagerEmp managerEmp = managerEmpService.findMangerEmp(empId, formerPassWord);
		if (managerEmp != null && managerEmp.getEmpId() != null) {
			managerEmpService.updateManger(newPassword,empId);
			managerEmp.setPassWord(newPassword);
			HttpSession session = request.getSession(true);
			session.setMaxInactiveInterval(20 * 60);// 有效期20分钟
			session.setAttribute("managerEmp", managerEmp);
			return "/jsp/power/results";
		} else {
			User user = userPowerService.findUserId(empId);
			modelMap.addAttribute("empId",user.getEmpId());
			modelMap.addAttribute("emp_name",user.getEmp_name());
			modelMap.addAttribute("error", "旧密码错误");
			return "/jsp/power/updatePassWord";
		}
	}
	/**
	 * 查看管理员
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/managerCheck.power", method = RequestMethod.GET)
	public String managerCheck(ModelMap modelMap, HttpServletRequest request){
		HttpSession session = request.getSession();
		ManagerEmp managerEmp = (ManagerEmp) session.getAttribute("managerEmp");
		if(managerEmp != null){
			List<ManagerList> managerList = managerEmpService.getManagerList();
			modelMap.addAttribute("managerList",managerList);
			if(managerEmp.getGrade().equals("1")){
				modelMap.addAttribute("govern",true);
			}else {
				modelMap.addAttribute("govern",false);
			}
			return "/jsp/power/managerCheck";
		}else{
			return "";
		}
		
	}
	/**
	 * session注销
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/logoff.power", method = RequestMethod.GET)
	public String logoff(HttpServletRequest request){
		HttpSession session = request.getSession();
//		session.invalidate();
		session.removeAttribute("managerEmp");
		//System.out.println("注销了");
//		return "redirect:/jsp/power/managlogin.jsp";
		return "redirect:/manage/login.jsp";
	}
	
	public static void main(String[] args) {
		String str = "河北分公司|协管责任人|asdf";
		if (str.indexOf("责任人") > 0) {
			//System.out.println(str.lastIndexOf("|"));
			str = str.substring(0, str.lastIndexOf("|"));
			System.out.println(str);

		}
	}
}
